﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    public sealed partial class LoadingState : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        int[] loadingBar;
        bool doneLoading;

        public LoadingState(Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            this.spriteBatch = spriteBatch;
            loadingBar = new int[4] { 28, Game.GraphicsDevice.Viewport.Height - (int)((Game.GraphicsDevice.Viewport.Height) * 0.035), Game.GraphicsDevice.Viewport.Width - (int)((Game.GraphicsDevice.Viewport.Width) * 0.028 * 2), 20 };
            doneLoading = false;
        }

        public sealed override void Update(GameTime gameTime)
        {
            if (StateManager.currentState == States.LOADING)
            {
                ResourceManager.loadNextResource();
                MouseState mouseState = Mouse.GetState();
                if (!doneLoading && ResourceManager.getLoadingProgress() >= 1.0f)
                {
                    GameVariables.readConfig();
                    Game.Components.Add(new MainMenuState(Game, spriteBatch));
                    Game.Components.Add(new OptionsState(Game, spriteBatch));
                    Game.Components.Add(new GameOverState(Game, spriteBatch));
                    Game.Components.Add(new PauseState(Game, spriteBatch));
                    Game.Components.Add(new PauseOptionsState(Game, spriteBatch));
                    Game.Components.Add(new PlayGameState(Game, spriteBatch));
                    MediaPlayer.IsRepeating = true;
                    ResourceManager.getSound("LoadingDone").Play();
                    MediaPlayer.Volume = 1;
                    if (GameVariables.music&&!GameVariables.skipMenu)
                    {
                        MediaPlayer.Play(ResourceManager.getSong("menuSong"));
                    }
                    doneLoading = true;
                }
                else if (doneLoading &&
                    Keyboard.GetState().GetPressedKeys().Length > 0||mouseState.LeftButton==ButtonState.Pressed)
                {
                    if (GameVariables.skipMenu)
                    {
                        StateManager.currentState = States.PLAYGAME;
                    }
                    else
                    {
                        StateManager.currentState = States.MAINMENU;
                    }
                }
            }
        }

        public sealed override void Draw(GameTime gameTime)
        {
            if (StateManager.currentState == States.LOADING)
            {
                var t = new Texture2D(Game.GraphicsDevice, 1, 1);
                t.SetData(new[] { Color.White });
                spriteBatch.Begin();
                spriteBatch.Draw(ResourceManager.getTexture("Loading"), Game.GraphicsDevice.Viewport.TitleSafeArea, Color.White);
                spriteBatch.Draw(t, new Rectangle(loadingBar[0], loadingBar[1], (int)(loadingBar[2]*ResourceManager.getLoadingProgress()), loadingBar[3]), Color.Orange);
                if (ResourceManager.getLoadingProgress() >= 1.0f)
                {
                    spriteBatch.DrawString(ResourceManager.getFont("JingJing"), "Done, Press Any Key...", new Vector2(50, Game.GraphicsDevice.Viewport.Height - (int)(Game.GraphicsDevice.Viewport.Height * 0.16)), Color.Orange);
                }
                else
                {
                    spriteBatch.DrawString(ResourceManager.getFont("JingJing"), "Loading...", new Vector2(50, Game.GraphicsDevice.Viewport.Height - (int)(Game.GraphicsDevice.Viewport.Height*0.16)), Color.Orange);
                }
                spriteBatch.End();
            }
        }
    }
}